package com.nuan.shiro.controller;

import com.nuan.shiro.constants.Result;
import com.nuan.shiro.constants.ResultUtil;
import com.nuan.shiro.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class TestController
{

    /**
     * 过期时间，默认一天
     */
    private long expTime=86400;

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(@RequestParam("username") String username, @RequestParam("password") String password){
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行认证登陆
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) {
            return "未知账户";
        } catch (IncorrectCredentialsException ice) {
            return "密码不正确";
        } catch (LockedAccountException lae) {
            return "账户已锁定";
        } catch (ExcessiveAttemptsException eae) {
            return "用户名或密码错误次数过多";
        } catch (AuthenticationException ae) {
            return "用户名或密码不正确！";
        }
        if (subject.isAuthenticated()) {
            return "登录成功";
        } else {
            token.clear();
            return "登录失败";
        }
    }

    @RequestMapping(value = "/loginjwt",method = RequestMethod.POST)
    public Result<String> login(@RequestBody Map<String,String> map){

        String token=null;
        //匹配数据库用户名和密码
        //匹配成功后用jwt生成token
        token= JwtUtils.buildJWT(map.get("username"),expTime);
        //拥有此token就可以经过网关校验了
        return ResultUtil.success(token);

    }

    @RequestMapping(value = "loginPage",method = RequestMethod.GET)
    public String loginloginPage(){
        return "nacos shiro index loginPage";
    }

    @RequestMapping(value = "get",method = RequestMethod.GET)
    public String get(){
        return "nacos shiro hello";
    }

    @RequestMapping(value = "admin/get",method = RequestMethod.GET)
    public String aget(){
        return "nacos shiro Admin get";
    }

    @RequestMapping(value = "admin/get1",method = RequestMethod.GET)
    public String aget1(){
        return "nacos shiro Admin get1";
    }

    @RequiresPermissions(value = {"admin:get"},logical = Logical.OR)
    @RequestMapping(value = "admin/get2",method = RequestMethod.GET)
    public String aget2(){
        return "nacos shiro Admin get1";
    }

    @RequestMapping(value = "nuan/add/get1",method = RequestMethod.GET)
    public String nuanaddAget1(){
        return "nacos shiro nuan add get1";
    }
    @RequestMapping(value = "nuan/add/get2",method = RequestMethod.GET)
    public String nuanaddAget2(){
        return "nacos shiro nuan add get2";
    }
    @RequestMapping(value = "nuan/get",method = RequestMethod.GET)
    public String nget(){
        return "nacos shiro nuan get";
    }
    @RequestMapping(value = "nuan/get1",method = RequestMethod.GET)
    public String nget1(){
        return "nacos shiro nuan get1";
    }

    @RequestMapping(value = "consumer/get1",method = RequestMethod.GET)
    public String consumerget1(){
        return "nacos shiro nuan consumerget1";
    }
    @RequestMapping(value = "consumer/get2",method = RequestMethod.GET)
    public String consumerget2(){
        return "nacos shiro nuan consumerget2";
    }

    @RequestMapping(value = "get1",method = RequestMethod.GET)
    public String get1(){
        return "nacos shiro hello1";
    }
    @RequestMapping(value = "get2",method = RequestMethod.GET)
    public String get2(){
        return "nacos shiro hello2";
    }
    @RequestMapping(value = "get3",method = RequestMethod.GET)
    public String get3(){
        return "nacos shiro hello3";
    }


    @RequestMapping(value = "nacos/roles",method = RequestMethod.GET)
    @RequiresRoles(value = {"nuan","admin"},logical = Logical.OR)
    public String roles(){
        return "nacos shiro roles";
    }


    @RequestMapping(value = "qw/pres",method = RequestMethod.GET)
    @RequiresPermissions(value = {"admin:get"},logical = Logical.OR)
    public String qw(){
        return "nacos shiro RequiresPermissions";
    }

    @RequestMapping(value = "nacos/pres",method = RequestMethod.GET)
    @RequiresRoles(value = {"ysnnuan"})
    @RequiresPermissions(value = {"ysnnuan:get"},logical = Logical.OR)
    public String Permis(){
        return "nacos shiro RequiresPermissions";
    }



}
